package com.system.integralserver.controller;


import com.alibaba.fastjson.JSONObject;
import com.system.commonutils.bean.ApiResult;
import com.system.commonutils.bean.PagesBean;
import com.system.commonutils.pojo.UsePoints;
import com.system.integralserver.pojo.IntegralInfo;
import com.system.integralserver.pojo.Orders;
import com.system.integralserver.service.IntegralService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ll
 * Date: 2020/07/09 14:29
 * Description:
 * Version: V1.0
 */
@RestController
@Api(value = "积分管理",tags = "积分管理-获取积分API")
@RequestMapping(value = "/integral")
public class IntegralConsumer {
    @Autowired
    AmqpTemplate amqpTemplate;

    @Autowired
    IntegralService integralService;

    @RabbitListener(queues = "orderserverIntegral")
//    @RequestMapping(value = "/queryGoods",method = {RequestMethod.POST},
//            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    @ApiOperation(value = "生成积分接口",notes = "根据RabbitMQ消息查询订单号并生成积分",httpMethod = "POST", response = ApiResult.class)
    public ApiResult getIntegral(Message msg){
        System.out.println("订单id为：" + msg);
        //msg.getBody().toString() 返回的是hashcode，使用下面那种方法
        String order_ids = new String(msg.getBody());
        System.out.println("=======订单：" + order_ids);
        JSONObject jsonArray = JSONObject.parseObject(order_ids);
        int order_id = Integer.valueOf(jsonArray.getIntValue("orderId"));
        Orders orders = integralService.getMessage(order_id);
        IntegralInfo integralInfo = new IntegralInfo();
        integralInfo.setIntegralInfoCreateTime(orders.getOrdersCreateTime());
        integralInfo.setIntegralInfoMoney(orders.getOrdersTotlePrice());
        double total_price = orders.getOrdersTotlePrice();
        int integral = (int)Math.floor(total_price / 1);
        integralInfo.setIntegralInfoIntegral(integral);
        integralInfo.setIntegralInfoOrderNo(orders.getOrdersNo());
        int result = integralService.createIntegral(integralInfo);
        if(result > 0){
//            ApiResult.success("生成积分成功！");
            System.out.println("生成积分成功！");
        }else{
//            ApiResult.failure("生成积分失败！");
            System.out.println("生成积分失败！");
        }
        return null;
    }

    @RequestMapping(value = "/useIntegral",method = {RequestMethod.POST},
                produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "消费积分接口",notes = "优惠券兑换积分",httpMethod = "POST", response = ApiResult.class)
    public ApiResult useIntegral(UsePoints usePoints){
        System.out.println("UsePoints:" + usePoints);
        int result = integralService.useIntegral(usePoints);
        if(result > 0){
            return ApiResult.success("添加成功！");
        }else{
            return ApiResult.failure("添加失败！");
        }
    }

    @RequestMapping(value = "/queryPoints",method = {RequestMethod.POST},
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "查询积分接口",notes = "根据订单号生成积分",httpMethod = "POST",response = ApiResult.class)
    public ApiResult<PagesBean<UsePoints>> queryIntegralInfo(UsePoints usePoints) {
        PagesBean pagesBean = new PagesBean();
        System.out.println("controller==" + usePoints);
        pagesBean.setNowsPage(usePoints.getNowsPage());
        pagesBean.setPageSize(usePoints.getPageSize());
        List<UsePoints> list = integralService.queryIntegralInfo(usePoints);
        System.out.println("-=-=-=-=-=-" + list);
        if (list.size() > 0) {
            int totalSize = integralService.countInfo(usePoints);
            pagesBean.setDataCount(totalSize);
            pagesBean.setData(list);
            return ApiResult.successWithData("查询成功！", pagesBean);
        } else {
            return ApiResult.failure("查询失败！");
        }
    }

}
